//
//  ContactTableViewCell.swift
//  BusinessIOS
//
//  Created by Rain on 2018/11/6.
//  Copyright © 2018 XingJian Li. All rights reserved.
//

import UIKit
import MapKit
class ContactTableViewCell: UITableViewCell {

    var map:MKMapView!
    
    var titleLabel:UILabel!
    
    var bgLabel:UILabel!
    
    var PhoneLabel:UILabel!
    
    var AdressLabel:UILabel!
    
    var MailLabel:UILabel!
    
    
    var center1 :CLLocation!
    
     var currentRegion:MKCoordinateRegion!
    
    
    
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        
    }
    // 赋值方法 - 显示 cell 内容方法
     func showAppInfoWithModel(model: ContactModel) {
        
        
        titleLabel.text = model.company
         PhoneLabel.text = "联系电话:\(model.tel!)"
        
         AdressLabel.text = "联系地址:\(model.address!)"
        
        MailLabel.text = "联系邮箱:\(model.email!)"
        
//        center = CLLocation(latitude: model.latitude, longitude: model.longitude)
        
        //创建一个MKCoordinateSpan对象，设置地图的范围（越小越精确）
        
     
        let latDelta = 0.05
        let longDelta = 0.05
        let currentLocationSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
        
        center1 = CLLocation(latitude: Double(model.latitude)!, longitude: Double(model.longitude)!)
        currentRegion = MKCoordinateRegion(center: center1.coordinate,span: currentLocationSpan)
        map.setRegion(currentRegion, animated: true)
        
        //创建一个大头针对象
        let objectAnnotation = MKPointAnnotation()
        //设置大头针的显示位置
        objectAnnotation.coordinate = CLLocation(latitude: Double(model.latitude)!, longitude: Double(model.longitude)!).coordinate
        //设置点击大头针之后显示的标题
        objectAnnotation.title = model.company
      
        //添加大头针
        map.addAnnotation(objectAnnotation)
 
      }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        
        map = MKMapView()
   
        map.mapType = MKMapType.standard
        
       
//        let center:CLLocation = CLLocation(latitude: 32.029171, longitude: 118.788231)
//        let currentRegion:MKCoordinateRegion = MKCoordinateRegion(center: center1.coordinate,
//                                                                  span: currentLocationSpan)
     

        self.contentView.addSubview(map)
        
        
        titleLabel = UILabel()
        titleLabel.backgroundColor = UIColor.white
        titleLabel.text = "曲靖市招商局"
        titleLabel.textAlignment = .left
        titleLabel.font = UIFont.systemFont(ofSize:15)//一般方法
        self.contentView.addSubview(self.titleLabel!)
        
        
        bgLabel = UILabel()
        bgLabel.backgroundColor = UIColor.groupTableViewBackground
//        bgLabel.text = "省招商局与云南财经大学共同举办校友会"
//        bgLabel.textAlignment = .center
//        bgLabel.font = UIFont.systemFont(ofSize:15)//一般方法
        self.contentView.addSubview(self.bgLabel!)
        
        PhoneLabel = UILabel()
        PhoneLabel.backgroundColor = UIColor.white
        PhoneLabel.text = "联系电话:0874-3135230"
        PhoneLabel.textAlignment = .left
        PhoneLabel.font = UIFont.systemFont(ofSize:12)//一般方法
        self.contentView.addSubview(self.PhoneLabel!)
        
        
        AdressLabel = UILabel()
        AdressLabel.backgroundColor = UIColor.white
        AdressLabel.text = "联系地址:云南省曲靖市麒麟区文昌街"
        AdressLabel.textAlignment = .left
        AdressLabel.font = UIFont.systemFont(ofSize:12)//一般方法
        self.contentView.addSubview(self.AdressLabel!)
        
        
        MailLabel = UILabel()
        MailLabel.backgroundColor = UIColor.white
        MailLabel.text = "联系邮箱:qujingzs@163.com"
        MailLabel.textAlignment = .left
        MailLabel.font = UIFont.systemFont(ofSize:12)//一般方法
        self.contentView.addSubview(self.MailLabel!)
        
        
        
        
        self.layoutView()
        
    }
    func layoutView(){
        
        self.map.snp.makeConstraints { (make) in
            make.top.equalTo(self.snp.top).offset(5)
            make.left.equalTo(self.snp.left).offset(10)
            make.right.equalTo(self.snp.right).offset(-10)
            make.height.equalTo(150)
        }
        self.titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self.map.snp.bottom).offset(0)
            make.left.equalTo(self.snp.left).offset(10)
            make.right.equalTo(self.snp.right).offset(-10)
            make.height.equalTo(30)
        }
        
        self.bgLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self.titleLabel.snp.bottom).offset(0)
            make.left.equalTo(self.snp.left).offset(10)
            make.right.equalTo(self.snp.right).offset(-10)
            make.height.equalTo(1)
        }
        
        self.PhoneLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self.bgLabel.snp.bottom).offset(0)
            make.left.equalTo(self.snp.left).offset(10)
            make.right.equalTo(self.snp.right).offset(-10)
            make.height.equalTo(20)
        }
        
        self.AdressLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self.PhoneLabel.snp.bottom).offset(0)
            make.left.equalTo(self.snp.left).offset(10)
            make.right.equalTo(self.snp.right).offset(-10)
            make.height.equalTo(20)
        }
        
        self.MailLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self.AdressLabel.snp.bottom).offset(0)
            make.left.equalTo(self.snp.left).offset(10)
            make.right.equalTo(self.snp.right).offset(-10)
            make.bottom.equalTo(self.snp.bottom).offset(-10)
        }
        
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
